# -*- coding: utf-8 -*-

# File Name： parse
# Description :
# Author : lirui
# create_date： 2022/7/26
# Change Activity:
from fastapi import UploadFile

import ml
import ml.cv
from config.default_config import upload_file_dir

ABS_TMP_IMAGE_PATH = ml.join_relative_path(upload_file_dir)


def parse_image_from_request(file: UploadFile):
    """

    Args:
        file:

    Returns:

    """
    contents = file.file.read()
    ml.create_dir(ABS_TMP_IMAGE_PATH)
    img_path = ml.join_path(ABS_TMP_IMAGE_PATH, ml.get_uuid_str() + '.jpg')
    with open(img_path, 'wb') as f:
        f.write(contents)
    img = ml.cv.read_img(img_path)
    ml.delete_file(img_path)
    return img
